/**
	Copyright (c) 2010 ATTi 
	All rights reserved.
	Author: Michael Shu
	Test coverage : CaseFieldsUpdateFromOwnerHelper.cls
	Date: 1/21/2011
*/
@isTest

private class CaseFieldsUpdateFromOwnerTester {
	private static map<String, User> users = new map<String, User>();
	private static User sysAdmins;
	
	private static void addUsers(){
		list<Profile> profiles = [select ID, Name from Profile where Name  like '%IOG%'];
		UserRole[] userrole = [select ID, Name from UserRole where Name like '%IOG%' limit 2];
		list<User> user_insert = new List<User>();
		Integer i = 0;
		for(UserRole r : userrole){
			User u_new = new User();
			u_new.UserName = 'xyz_1234567890@atti-temp.com' + i;
			u_new.LastName = 'Michael Shu Test Email' + i;
			u_new.Email = 'xyz_1234567890@atti.com';
			u_new.Alias = 'abcde' + i;
			u_new.CommunityNickname = 'xyaz_1234556' + i;
			u_new.TimeZoneSidKey = 'America/Los_Angeles';
			u_new.LocaleSidKey = 'en_US';
			u_new.EmailEncodingKey = 'UTF-8';
			u_new.ProfileId = profiles[0].Id;
			u_new.UserRoleId = r.Id;
			u_new.LanguageLocaleKey = 'en_US';
			u_new.Office__c = 'test office' + i;
			u_new.Sales_Region__c = 'test sales region' + i;
			u_new.Cycle_Team__c = 'Cycle Team Test ' + i;
			user_insert.add(u_new);
			i++;
		}
		Database.Saveresult[] lsr = Database.insert(user_insert);
		Set<ID> uIDs = new Set<ID>();
		for(Database.Saveresult sr : lsr){
			uIDs.add(sr.getID());
		}
		List<User> userList = [select ID, Name, Profile.Name, UserRole.Name, Cycle_Team__c from User where ID IN: uIDs];
		for(User u : userList){
			users.put(u.UserRole.Name, u);
		}	
		User u_new = new User();
		u_new.UserName = 'xyz_1234567890@atti-temp.com';
		u_new.LastName = 'Michael Shu Admin Test';
		u_new.Email = 'admin-null@atti.com';
		u_new.Alias = 'admin_me';
		u_new.CommunityNickname = 'admin123456' + i;
		u_new.TimeZoneSidKey = 'America/Los_Angeles';
		u_new.LocaleSidKey = 'en_US';
		u_new.EmailEncodingKey = 'UTF-8';
		u_new.ProfileId = [select ID from Profile where Name =: 'System Administrator' limit 1].Id;
		u_new.UserRoleId = [select ID from UserRole where Name =: 'Information Systems' limit 1].Id;
		u_new.LanguageLocaleKey = 'en_US';
		Database.Saveresult u_sr = Database.insert(u_new);	
		sysAdmins = [select ID from User where ID =: u_sr.getID() limit 1];
	}	
		
	static testMethod void testCaseOwnerChange() {
		addUsers();
		System.assertEquals(2, users.size());
		Case c = new Case();
		c.RecordTypeId = RecordTypeHelper.GetRecordTypeID('Case',  'IOG Incident');
		c.OwnerId = users.values()[0].Id;
		Database.Saveresult c_sr;
		System.runAs(sysAdmins){
			c_sr = Database.insert(c);
		}
		Case c_rst = [select Cycle_Team__c from Case where Id =: c_sr.getId()];
		System.assertEquals(c_rst.Cycle_Team__c, users.values()[0].Cycle_Team__c);
		c_rst.OwnerId = users.values()[1].Id;
		Database.Saveresult c_sr_update; 
		System.runAs(sysAdmins){
			c_sr_update = Database.update(c_rst);
		}
		Case c_rst_update = [select Previous_Owner__c, Cycle_Team__c from Case where Id =: c_sr_update.getId()];
		System.assertEquals(c_rst_update.Cycle_Team__c, users.values()[1].Cycle_Team__c);
		System.assertEquals(c_rst_update.Previous_Owner__c, users.values()[0].Id);
	}
}